package customer.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

/**
 * Servlet implementation class GetCustomerByIdServlet
 */
public class GetCustomerByIdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetCustomerByIdServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = response.getWriter();
		//ID로 고객 정보를 조회
		String id = request.getParameter("id");
		ServletContext ctx = getServletContext();
		BasicDataSource ds = (BasicDataSource)ctx.getAttribute("dataSource");
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		String sql = "select * from customer where resident_number=?";
		try{
			conn = ds.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, id);
			rset = pstmt.executeQuery();
			
			if(rset.next()){
				out.println("주민번호 : " + rset.getString(1) + "<br />");
				out.println("이름 : " + rset.getString(2) + "<br />");
				out.println("주소 : " + rset.getString(3) + "<br />");
				out.println("전화번호 : " + rset.getString(4) + "<br />");
				out.println("연봉: " + rset.getInt(5) + "<br />");
			}else{
				out.println("조회하려는 고객이 없습니다.");
			}
		}catch(SQLException e){
			e.printStackTrace();
			out.println("오류가 발생했습니다. 메세지 : " + e.getMessage());
		}finally{
		}
	}

}
